home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / develop issue 28 / develop issue 28 code / macapp debugging / twistdownlists / uprogressindicators.h < prev    next >
Encoding:
Text File  |  1996-07-15  |  8.5 KB  |  276 lines

  1. //----------------------------------------------------------------------------------------
  2. // UProgressIndicators.h
  3. // ETO20 MacApp 3.3.1, MPW 3.4.1
  4. // Copyright ©1995-1996 Conrad Kopala
  5. // Twist Down Lists version 2.0a0 7/15/96
  6. //Reference: A Reassuring Progress Indicator for MacApp by James Plamodon
  7. //FrameWorks Volume 5, Number 3, June 1991, page 46
  8. //----------------------------------------------------------------------------------------
  9. /*
  10. Instructions for use of TProgressBarIndicator
  11.  
  12. Add the following fields to the TYourObject that will be using the TProgressBarIndicator;
  13. TProgressBarIndicator* fProgressBarIndicator;
  14. long fUpdateAmount;
  15. long fProgressUpdateCounter;
  16.  
  17. In TYourObject::TYourObject() initialze them as follows:
  18. fProgressBarIndicator = NULL;
  19. fUpdateAmount = 0;
  20. fProgressUpdateCounter = 0;
  21.  
  22. In the method of TYourObject that uses TProgressBarIndicator insert the following code:
  23.  
  24.     CStr255 itsOperationString;
  25.     GetIndString(itsOperationString, kProgressIndicatorOperationStrings, 3);
  26.  
  27.     CStr255 itsSubjectString = fVolumeName;
  28.     fProgressBarIndicator = NewProgressBarIndicator(itsOperationString, itsSubjectString);
  29.  
  30.     this -> AddDependent(fProgressBarIndicator);
  31.     fProgressBarIndicator -> PrepareToStart(TRUE);
  32.  
  33.     long barLength = fProgressBarIndicator -> GetBarLength();
  34.  
  35. //The default value for fUpdateAmount is set to 1.                                
  36. //Unfortunately, if there are a lot of items to read and the progress bar is updated each time 
  37. //an item is read, it takes a long time to populate the list. With the following if statement,
  38. //the update amount is adjusted so that the progress bar is updated only when enough
  39. //progress has been made to update it by at least one pixel. This noticibly speeds things up. 
  40. //Comment out the following if statement and see for yourself. 
  41.  
  42.     if (fNumberOfItemsToLoad > barLength)
  43.         {
  44.             long updateAmount = fNumberOfItemsToLoad/barLength;    
  45.             this -> SetUpdateAmountTo(updateAmount);    
  46.         }
  47.     
  48.     progressUpdateCounter = 0;
  49.  
  50.     fProgressBarIndicator -> Start(fNumberOfItemsToLoad);
  51.  
  52.     You are now ready to use fProgressBarIndicator.
  53.     
  54. In the loop which does whatever is being done insert the following code:
  55.  
  56. do 
  57. {    
  58.         ++fProgressUpdateCounter;
  59.         if (fProgressUpdateCounter >= fUpdateAmount)
  60.             {
  61.                 fProgressUpdateCounter = 0;
  62.                 this -> Changed(mUpdateProgressBar, this);
  63.                 this -> SetChangeCount(0);    //If this refers to a TDocument, changed sets fChangeCount to 
  64.                                                                         //a positive value which will result in an attempt to save the 
  65.                                                                         //document when it is closed and if you don't want that, 
  66.                                                                         //set fChangecount to zero here. Or, override TDocument::Changed
  67.                                                                         //and don't call the inherited method.
  68.             }
  69.  
  70. while (fProgressBarIndicator -> IsStopped() == FALSE);
  71.  
  72. Whe you're done with fProgressBarIndicator, get rid of it:
  73.     if (fProgressBarIndicator != NULL)
  74.             {
  75.                 fProgressBarIndicator -> Stop();
  76.                 this -> RemoveDependent(fProgressBarIndicator);
  77.                 fProgressBarIndicator = (TProgressBarIndicator*)FreeIfObject(fProgressBarIndicator);
  78.             }        
  79. */
  80.  
  81.  
  82. #ifndef __UPROGRESSINDICATORS__
  83. #define __UPROGRESSINDICATORS__
  84.  
  85. //MacApp stuff
  86. /*
  87. #ifndef __UOBJECT__
  88. #include "UObject.h"
  89. #endif
  90. */
  91. #ifndef __UCONTROL__
  92. #include "UControl.h"
  93. #endif
  94.  
  95. #ifndef __UDIALOG__
  96. #include "UDialog.h"
  97. #endif
  98.  
  99. #ifndef __UDIALOGBEHAVIOR__
  100. #include "UDialogBehavior.h"
  101. #endif
  102.  
  103. #ifndef __UVIEWSERVER__
  104. #include "UViewServer.h"
  105. #endif
  106.  
  107. #ifndef __UWINDOW__
  108. #include "UWindow.h"
  109. #endif
  110.  
  111. //ToolBox stuff
  112. //None
  113.  
  114. //ANSI stuff
  115. //None
  116.  
  117. //----------------------------------------------------------------------------------------
  118. // Constants
  119. //----------------------------------------------------------------------------------------
  120. const ResNumber kProgressIndicatorOperationStrings    = 1003;
  121. const IDType kProgressBarBehavior = 'pBar';
  122.  
  123. //----------------------------------------------------------------------------------------
  124. //  Forward and external classes
  125. //----------------------------------------------------------------------------------------
  126. class TProgressBar;            //forward
  127. class TProgressView;        //forward
  128.  
  129. //----------------------------------------------------------------------------------------
  130. // TProgressBarIndicator
  131. //----------------------------------------------------------------------------------------
  132.  
  133. class TProgressBarIndicator: public TObject
  134. {
  135.     MA_DECLARE_CLASS;
  136.     
  137. public:
  138. long fMax;
  139. long fCurrent;
  140. Boolean fStopped;
  141. TWindow* fProgressWindow;
  142. TProgressView* fProgressView;
  143. CStr255 fOperationString;
  144. CStr255 fSubjectString;
  145. long fUpdateAmount;
  146.  
  147. TProgressBarIndicator::TProgressBarIndicator();
  148. virtual ~TProgressBarIndicator();
  149.  
  150. virtual void TProgressBarIndicator::IProgressBarIndicator(const CStr255& operationString, const CStr255& subjectName);
  151. void TProgressBarIndicator::CreateProgressWindow();
  152.  
  153. virtual void TProgressBarIndicator::Continue();
  154.  
  155. virtual void TProgressBarIndicator::DoUpdate(ChangeID theChange, 
  156.                                 TObject* changedObject, TObject* changedBy,TDependencySpace* dependencySpace);
  157.                                 
  158. virtual void TProgressBarIndicator::PrepareToStart(Boolean redraw);
  159. virtual void TProgressBarIndicator::Reset();
  160. virtual void TProgressBarIndicator::Start(long max);
  161. virtual void TProgressBarIndicator::Stop();
  162. virtual Boolean TProgressBarIndicator::IsStopped();
  163. virtual void TProgressBarIndicator::DoContinue(long howMuch);
  164. virtual void TProgressBarIndicator::DrawAtOnce(Boolean wholeThing);
  165.  
  166. virtual Boolean TProgressBarIndicator::WasCancelled();
  167. long TProgressBarIndicator::GetBarLength();
  168. void TProgressBarIndicator::SetUpdateAmountTo(long updateAmount);
  169. };
  170.  
  171. //----------------------------------------------------------------------------------------
  172. // TProgressDialogBehavior
  173. //----------------------------------------------------------------------------------------
  174.  
  175. class TProgressDialogBehavior: public TDialogBehavior
  176. {
  177.     MA_DECLARE_CLASS;
  178.     
  179. public:
  180.  
  181. TProgressDialogBehavior::TProgressDialogBehavior();
  182. virtual ~TProgressDialogBehavior();
  183.  
  184. void TProgressDialogBehavior::IProgressDialogBehavior(Boolean isModal,IDType  itsDefaultItem,IDType  itsCancelItem);
  185. Boolean TProgressDialogBehavior::WasCancelled();
  186.  
  187.  
  188. };
  189.  
  190. //----------------------------------------------------------------------------------------
  191. // TProgressWindow
  192. //----------------------------------------------------------------------------------------
  193.  
  194. class TProgressWindow: public TWindow
  195. {
  196.     MA_DECLARE_CLASS;
  197.     
  198. public:
  199. TProgressWindow::TProgressWindow();
  200. virtual ~TProgressWindow();
  201.  
  202. void TProgressWindow::IProgressWindow(TDocument* itsDocument,WindowPtr itsWMgrWindow,
  203.                                           Boolean canResize,Boolean canClose,Boolean disposeOnFree);
  204.  
  205.  
  206.  
  207.  
  208. };
  209. //----------------------------------------------------------------------------------------
  210. // TProgressView
  211. //----------------------------------------------------------------------------------------
  212.  
  213. class TProgressView: public TView
  214. {
  215.     MA_DECLARE_CLASS;
  216.     
  217. public:
  218. TProgressBar* fProgressBar;
  219. TButton* fStopButton;
  220.  
  221. TProgressView::TProgressView();
  222. virtual ~TProgressView();
  223.  
  224. void TProgressView::IProgressView(TDocument* itsDocument,TView* itsSuperView,
  225.                                                                      const VPoint& itsLocation,const VPoint& itsSize,
  226.                                                                   SizeDeterminer itsHSizeDet,SizeDeterminer itsVSizeDet);
  227.  
  228. void TProgressView::Continue(long howMuch);
  229. void TProgressView::DrawAtOnce(Boolean redraw);
  230. void TProgressView::PrepareToStart(Boolean redraw);
  231. void TProgressView::Reset();
  232. void TProgressView::Start(long max);
  233. void TProgressView::Stop();
  234. long TProgressView::GetBarLength();
  235. };
  236. //----------------------------------------------------------------------------------------
  237. // TProgressBar
  238. //----------------------------------------------------------------------------------------
  239.  
  240. class TProgressBar: public TControl
  241. {
  242.     MA_DECLARE_CLASS;
  243.     
  244. public:
  245. long fMax;
  246. long fCurrent;
  247. VRect fBarRect;
  248. PenState fPen;
  249.  
  250. TProgressBar::TProgressBar();
  251. virtual ~TProgressBar();
  252. void TProgressBar::IProgressBar(TView* itsSuperView,const VPoint& itsLocation,const VPoint& itsSize,
  253.                         SizeDeterminer itsHSizeDet,SizeDeterminer itsVSizeDet,const TextStyle& itsTextStyle);
  254.  
  255. void TProgressBar::SetBarSize();
  256. void TProgressBar::SetPen();
  257. virtual void TProgressBar::Draw(const VRect& area);
  258.  
  259. void TProgressBar::Continue(long howMuch);
  260. void TProgressBar::Reset();
  261. void TProgressBar::Start(long max);
  262. void TProgressBar::Stop();
  263. long TProgressBar::GetBarLength();
  264. };
  265.  
  266. //----------------------------------------------------------------------------------------
  267. // Global initialization procedure
  268. //----------------------------------------------------------------------------------------
  269.  
  270.  
  271. extern void InitUProgressDialog();
  272. extern TProgressBarIndicator* NewProgressBarIndicator(const CStr255& itsOperationString, const CStr255& itsSubjectString);
  273.  
  274. #endif
  275.